package listTest;

import java.util.Arrays;

public class MyArrayList implements IList{

    private int[] array;
    private int usedSize = 0;
    public static final int capacity = 10;

    public MyArrayList(){

        this.array = new int[capacity];
    }

    public void display(){
        for(int i = 0; i < this.usedSize; i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    //返回值是字符串数组
    private void grow(){
        //copyOf函数的返回值是字符数组类型，为什么可以这直接赋值
        this.array = Arrays.copyOf(this.array, 2*this.array.length);
    }

    //1、不能使用private， 因为在IList接口中也有isFull函数;

    public boolean isFull(){

        if(this.array.length == this.usedSize){
            return true;
        }

        return false;
    }

    public void add(int data){

        if(isFull()){
            grow();
        }
        this.array[usedSize] = data;
        this.usedSize++;
    }

    public void checkPos1(int pos){
        if(pos < 0 || pos > usedSize){
            throw new PosIllegalException("pos的数值不合理！！");
        }
    }

    public void add(int pos, int data){

        if(isFull()){
            grow();
        }

        try {
            checkPos1(pos);
            for(int i = usedSize-1; i >= pos; i--){

                this.array[i+1] = this.array[i];
            }
            this.array[pos] = data;
            this.usedSize++;
        }catch (PosIllegalException e){
            e.printStackTrace();;
        }

    }


    // 判定是否包含某个元素
    public boolean contains(int toFind){

        for(int i = 0; i < this.usedSize; i++){
            if(this.array[i] == toFind){
                return true;
            }
        }
        return false;
    }


    // 查找某个元素对应的位置
    public int indexOf(int toFind) {

        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }


    public void checkPos2(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new PosIllegalException("pos的数值不合理！！");
        }
    }

    public void checkEmpty(){
        if(usedSize == 0){
            throw new EmptyException("该顺序表为空！");
        }
    }

    //需要检查pos的数值是否符合数组的长度要求
    // 获取 pos 位置的元素
    public int get(int pos){

        try{
            checkPos2(pos);
            checkEmpty();
            return array[pos];

        }catch (EmptyException e){
            e.printStackTrace();;
        }catch ( PosIllegalException e) {
            e.printStackTrace();;
        }
        return -1;
    }



    // 给 pos 位置的元素设为 value
    public void set(int pos, int value){

        try{
            checkPos2(pos);
            checkEmpty();
            array[pos] = value;

        }catch (EmptyException e){
            e.printStackTrace();;
        }catch ( PosIllegalException e) {
            e.printStackTrace();;
        }

    }



    //删除第一次出现的关键字key
    public void remove(int toRemove){

        int num = 0;
        for (int i = 0; i < usedSize; i++) {
            if(array[i] == toRemove) {
                num = i;
            }
        }

        for (int i = num; i < usedSize-1; i++) {
            array[i] = array[i+1];
        }
        usedSize--;
    }


    // 获取顺序表长度
    public int size(){

        return this.usedSize-1;
    }



    // 清空顺序表
    public void clear(){
        usedSize = 0;

    }


}
